package com.study.services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IAddresdetailDAO;
import com.study.model.jdao.IGoodDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IOrderdetailDAO;
import com.study.model.jdao.IOrderformDAO;
import com.study.model.mdao.AddressdetailMapper;
import com.study.model.mdao.CustomerMapper;
import com.study.model.mdao.DeliverdetailMapper;
import com.study.model.mdao.DeliveryscheduleMapper;
import com.study.model.mdao.DeportFormMapper;
import com.study.model.mdao.GoodDAO;
import com.study.model.mdao.LinkmanMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.OpportunityMapper;
import com.study.model.mdao.OrderFormDetailMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.mdao.ReturnmoreMapper;
import com.study.model.mdao.UserMDAO;
import com.study.model.pojo.Addresdetail;
import com.study.model.pojo.Good;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Orderdetail;
import com.study.model.pojo.Orderform;
import com.study.model.vo.Cart;
import com.study.model.vo.CartBox;
import com.study.util.CustormAll;
import com.study.util.MaxId;

/**
 * 订单服务层
 * 
 * @author 郭星
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderService {

	@Autowired
	IOrderformDAO dao;
	@Autowired
	OrderFormMapper orderFormMapper;
	@Autowired
	ILinkmanDAO ilnkmandao;
	@Autowired
	CategoryService categoryService;
	@Autowired
	GoodDAO goodsdao;
	@Autowired
	IOrderformDAO iOrderformDAO;
	@Autowired
	IOrderdetailDAO iOrderdetailDAO;
	@Autowired
	IGoodDAO iGoodDAO;
	@Autowired
	OrderFormDetailMapper rDetailMapper;
	@Autowired
	UserMDAO userMDAO;
	@Autowired
	CustomerMapper customerMapper;
	@Autowired
	CustormAll alls;
	@Autowired
	AddressdetailMapper addressdetailMapper;
	@Autowired
	OpportunityMapper oppmapper;
	@Autowired
	CustormAll custormall;
	@Autowired
	CustomerService customerservice;
	@Autowired
	OrderFormDetailMapper detailMapper;
	@Autowired
	DeliveryscheduleMapper deliveryscheduleMapper;
	@Autowired
	DeliverdetailMapper deliverdetailMapper;
	@Autowired
	OrderfromServices orderfromServices;
	@Autowired
	ReturnmoreMapper returnmoreMapper;
	@Autowired
	LinkmanMapper linkmanMapper;
	@Autowired
	IAddresdetailDAO addressDAO;

	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	DeportFormMapper deportFormMapper;

	/**
	 * 分页查询
	 */
	public List<Orderform> selectByPager(int pageNum, int pageSize) {
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<Orderform> list = (List<Orderform>) dao.findAll();
		System.out.println("大小" + list.size());
		return list;
	}

	/**
	 * 查询对应订单的总回款情况
	 * 
	 * @param id
	 * @return
	 */
	public BigDecimal huikuan(Integer id) {
		return orderFormMapper.findHuikuanByOid(id);
	}

	/**
	 * 查询已开发票回款
	 * 
	 * @param id
	 * @return
	 */
	public BigDecimal yeshuikuan(Integer id) {
		return orderFormMapper.findHuikuanByOidYes(id);
	}

	/**
	 * 带条件的查询订单
	 * 
	 * @param selects     条件头
	 * @param selectinput 输入的客户名
	 * @param selectend   （废除）
	 * @param currentPage 页码
	 * @param pagesize    页大小
	 * @param uid         用户id
	 * @return
	 */
	public List<JSONObject> selectByPagerSql(String selects, String selectinput, String selectend, int currentPage,
			int pagesize, int uid) {

		// 查询当前员工是老板还是普通员工
		String selectByBoss = userMDAO.selectByBoss(uid);
		if (selectByBoss != null && selectByBoss.length() > 0) {
			if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

				selectinput = "%" + selectinput + "%";
			}
			PageHelper.startPage(currentPage, pagesize);
			return orderFormMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, null, null, null);
		} else {
			// 普通员工则先获取，自己正管理的客户
			List<Integer> selectByUserid = customerMapper.selectByUserid(uid);

			// 存储此员工管理的所有的客户Id
			List<Integer> allids = new ArrayList<>();

			if (selectByUserid.size() > 0 && selectByUserid != null) {
				// 当查询的客户id不为空的时候才做
				/*
				 * for (Integer integer : selectByUserid) { allids.addAll(alls.findAll(integer,
				 * 2)); }
				 */
				if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

					selectinput = "%" + selectinput + "%";
				}
				PageHelper.startPage(currentPage, pagesize);
				return orderFormMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, allids, uid, 1);
			} else {
				return null;
			}

		}

	}

	/**
	 * 带条件的查询订单明细
	 * 
	 * @param selects     条件头
	 * @param selectinput 输入的客户名
	 * @param selectend   （废除）
	 * @param currentPage 页码
	 * @param pagesize    页大小
	 * @param uid         用户id
	 * @return
	 */
	public List<JSONObject> selectByPagerSqlOrderDetail(String selects, String selectinput, String selectend,
			int currentPage, int pagesize, int uid) {

		// 查询当前员工是老板还是普通员工
		String selectByBoss = userMDAO.selectByBoss(uid);
		if (selectByBoss != null && selectByBoss.length() > 0) {
			if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

				selectinput = "%" + selectinput + "%";
			}
			PageHelper.startPage(currentPage, pagesize);
			return detailMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, null, null, null);
		} else {
			// 普通员工则先获取，自己正管理的客户
			List<Integer> selectByUserid = customerMapper.selectByUserid(uid);

			// 存储此员工管理的所有的客户Id
			List<Integer> allids = new ArrayList<>();

			if (selectByUserid.size() > 0 && selectByUserid != null) {
				// 当查询的客户id不为空的时候才做
				for (Integer integer : selectByUserid) {
					allids.addAll(alls.findAll(integer, 2));
				}
				if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

					selectinput = "%" + selectinput + "%";
				}
				PageHelper.startPage(currentPage, pagesize);
				return detailMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, allids, uid, 1);
			} else {
				return null;
			}

		}

	}

	/**
	 * 添加一条订单概率信息
	 * 
	 * @param od
	 */
	public boolean addOrder(Orderform od) {
		boolean flag = false;
		try {
			dao.save(od);

			logforcustDAO.insert(od.getCustomer().getCusId(), new Date(), od.getUser().getUseId(), 2203,
					od.getOId() + "订单开始..", "");
			flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 删除订单那是
	 * 
	 * @param id
	 */
	public void delete(Integer id) {
		orderFormMapper.deletes(id);
	}

	/**
	 * 添加一条订单概率信息
	 * 
	 * @param od
	 */
	public int addOrderMapper(String titles, Integer costomers, Integer orderid, Integer cartyrs, Integer paytypes,
			Integer opportunity, Integer qmans, Integer givemansto, Date havetime, Date haveoldtime, Integer endmoney,
			Integer fills) {

		Linkman findByLinId = ilnkmandao.findByLinId(givemansto);
		Integer flag = null;
		try {

			Integer findMaxId = orderFormMapper.findMaxId();
			System.out.println(findMaxId);
			if (findMaxId != 0) {
				flag = findMaxId;
			}
			String id = MaxId.generateNewOrderID(flag);
			/*System.out.println(id + "***" + titles + "***" + costomers + "***" + orderid + "***" + cartyrs + "***"
					+ paytypes + "***" + opportunity + "***" + qmans + "***" + findByLinId.getAddresdetail().getAdId()
					+ "***" + havetime + "***" + haveoldtime + "***" + endmoney + "***" + fills);*/

			int doInsert = orderFormMapper.doInsert(id, titles, costomers, orderid, cartyrs, paytypes, opportunity,
					qmans, findByLinId.getAddresdetail()!=null?findByLinId.getAddresdetail().getAdId():null, havetime, haveoldtime, endmoney, fills, new Date());


			// 订单信息
			System.out.println(id);
			JSONObject findByIds = orderFormMapper.findByIds(Integer.parseInt(id));
			System.out.println("订单信息如下：" + findByIds);
			logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(), findByIds.getInteger("use_id"), 2203,
					findByIds.getInteger("o_id") + "订单开始..", "");
			flag = Integer.parseInt(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.err.println(flag);
		return flag;
	}

	/**
	 * 修改订单概要数据
	 * 
	 * @param titles
	 * @param costomers
	 * @param orderid
	 * @param cartyrs
	 * @param paytypes
	 * @param opportunity
	 * @param qmans
	 * @param givemansto
	 * @param havetime
	 * @param haveoldtime
	 * @param endmoney
	 * @param fills
	 * @return
	 */
	public boolean updateOrderMapper(Integer id, Integer status, String titles, Integer costomers, Integer orderid,
			Integer cartyrs, Integer paytypes, Integer opportunity, Integer qmans, Integer givemansto, Date havetime,
			Date haveoldtime, Integer endmoney, Integer fills) {

		Linkman findByLinId = ilnkmandao.findByLinId(givemansto);
		boolean flag = false;
		try {
			int doInsert = orderFormMapper.doUpdate(id, status, titles, costomers, orderid, cartyrs, paytypes,
					opportunity, qmans, findByLinId.getAddresdetail().getAdId(), havetime, haveoldtime, endmoney,
					fills);
			flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 查询对应id的订单信息
	 * 
	 * @param id
	 * @return
	 */
	public JSONObject findById(Integer id) {
		// 订单信息
		JSONObject findByIds = orderFormMapper.findByIds(id);
		// 再放入地址详情信息
		JSONArray arr = new JSONArray();
		JSONObject obje = new JSONObject();
		obje.put("adid", findByIds.getInteger("ad_id"));
		obje.put("adn", findByIds.getString("ad_man"));
		obje.put("adphone", findByIds.getString("ad_phone"));
		obje.put("adaddress", findByIds.getString("ad_address"));
		arr.add(obje);
		findByIds.put("lin", arr);

		// 放入所有人信息
		JSONArray arr2 = new JSONArray();
		JSONObject obje2 = new JSONObject();
		obje2.put("used", findByIds.getInteger("use_id"));
		obje2.put("usen", findByIds.getString("use_name"));
		arr2.add(obje2);
		findByIds.put("use", arr2);

		// 放入销售机会
		findByIds.put("oop", oppmapper.findByCusId(findByIds.getInteger("cus_id")));

		// 放入签约人
		List<Linkman> selectByName = customerservice.selectById(findByIds.getInteger("cus_id"));
		// findByIds.put("custo", selectByName);//放入联系人

		// 查询是否有退货
		List<JSONObject> findReturnByOid = returnmoreMapper.findReturnByOid(id + "");
		if (findReturnByOid != null && findReturnByOid.size() > 0) {
			findByIds.put("retu", "有退货");
		} else {
			findByIds.put("retu", "无退货");
		}

		// 是否有出库记录
		List<JSONObject> findByOidTotru = deportFormMapper.findByOidTotru(id + "");
		if (findByOidTotru != null && findByOidTotru.size() > 0) {
			findByIds.put("outs", 1);
			int len = findByOidTotru.size();
			int ses = 0;
			for (JSONObject jsonObject : findByOidTotru) {
				if (jsonObject.getDate("tdate") != null) {
					ses++;
				}
			}
			// 出库是否完成
			if (len == ses) {
				findByIds.put("yesouts", 1);
			} else {
				findByIds.put("yesouts", 0);
			}
		} else {
			findByIds.put("outs", 0);
			findByIds.put("yesouts", 0);
		}

		// 发货状态
		JSONObject findByOid = deliveryscheduleMapper.findByOid(id);
		if (findByOid != null) {
			if (findByOid.getInteger("ds_state") == 3017) {
				findByIds.put("fahuo", "部分发货");
			} else if (findByOid.getInteger("ds_state") == 3018) {
				findByIds.put("fahuo", "全部发货");
			} else {
				findByIds.put("fahuo", "未发货");
			}

		}

		System.out.println("订单对应的信息就在这了：" + findByIds);
		return findByIds;
	}

	/**
	 * 查询所有商品包括他的类别
	 * 
	 * @return
	 */
	public JSONArray findByTree() {
		List<JSONObject> selectAll = categoryService.selectAll();
		JSONArray jArray = new JSONArray();
		for (JSONObject jsonObject : selectAll) {
			// System.out.println(jsonObject);
			if (jsonObject.getInteger("pid") == 0) {
				// 拿到一级类别
				JSONObject onelv = new JSONObject();
				onelv.put("label", jsonObject.getString("cat_name"));
				JSONArray children = new JSONArray();
				// 获取当前一级类别的id
				int onesid = jsonObject.getInteger("cat_id");
				// System.out.println("一级："+jsonObject.getString("cat_name"));
				for (JSONObject twosgive : selectAll) {
					if (twosgive.getInteger("pid") == onesid) {
						// 拿到二级类别
						//System.out.println(twosgive);
						JSONObject twolv = new JSONObject();
						twolv.put("label", twosgive.getString("cat_name"));
						// 第二级的id
						int twosid = twosgive.getInteger("cat_id");
						// 查询对应类别的商品信息
						List<JSONObject> selectByCatid = goodsdao.selectByCatid(twosid);
						JSONArray threesarray = new JSONArray();
						for (JSONObject threes : selectByCatid) {
							// System.err.println(selectByCatid);
							JSONObject threeobj = new JSONObject();
							threeobj.put("id", threes.getInteger("goo_id"));
							threeobj.put("label", threes.getString("goo_name"));
							threesarray.add(threeobj);
						}
						twolv.put("children", threesarray);
						children.add(twolv);
					}

				}
				onelv.put("children", children);
				// 放入大集合中
				jArray.add(onelv);
			}

		}
		return jArray;
	}

	/**
	 * 查询订单明细并放入购物车
	 * 
	 * @param id
	 * @param cart
	 */
	public void findOrderDetail(Integer id, CartBox cart) {
		List<Cart> list = new ArrayList<>();
		List<JSONObject> findByOdidDetail = rDetailMapper.findByOdidDetail(id);
		for (JSONObject jsonObject : findByOdidDetail) {
			System.out.println("当前详单记录：" + jsonObject);
			com.study.model.vo.Good goods = goodsdao.findByGoodsIdByvo(jsonObject.getInteger("goo_id"));
			System.out.println(goods);
			Cart carts = new Cart();
			carts.setGoods(goods);// 放入商品
			carts.setQuatity(jsonObject.getInteger("od_num"));
			carts.setSum(jsonObject.getDoubleValue("od_sumprice"));
			// Orderdetail orderdetail = new Orderdetail();
			Orderdetail findByoid = rDetailMapper.findByoid(jsonObject.getInteger("o_id"));
			carts.setOrderdetail(findByoid);
			list.add(carts);
		}
		cart.setMap(list);
	}

	/**
	 * 添加订单明细
	 * 
	 * @param id
	 * @param cart
	 */
	public void addOrderDetail(Integer id, CartBox cart) {
		// 查询订单概述对象
		orderFormMapper.updateSumCount(cart.getPrice(), cart.getQuatitys(), id);
		List<Cart> map = cart.getMap();
		for (Cart cart2 : map) {

			rDetailMapper.insertinto(id, cart2.getGoods().getGooId(), cart2.getQuatity(),
					cart2.getGoods().getGooPrice(), cart2.getSum());
			// orderdetails.add(orderdetail);
		}
	}

	/**
	 * 删除对应id的订单详情
	 * 
	 * @param id
	 */
	public void deleteOrderDetail(Integer id) {
		rDetailMapper.deletOrderdetails(id);
	}

	/**
	 * 根据订单Id生成发货单
	 * 
	 * @param id
	 */
	public void addFahuo(Integer id) {
		// 获取订单对象
		JSONObject findByIds = orderFormMapper.findByIds(id);
		// 添加发货单
		Integer flag = null;
		Integer findMaxId = deliveryscheduleMapper.findMaxId();
		System.out.println(findMaxId);
		if (findMaxId != null && findMaxId != 0) {
			flag = findMaxId;
		}
		String dd = MaxId.generateNewOrderID(flag);
		System.out.println(dd);
		deliveryscheduleMapper.add(Integer.parseInt(dd), findByIds.getInteger("cus_id"), findByIds.getInteger("use_id"),
				findByIds.getInteger("o_id"));

		// 获取对应订单详情表信息
		List<JSONObject> findByOdidDetail = detailMapper.findByOdidDetail(id);

		// 添加发货详情表信息
		for (JSONObject jsonObject : findByOdidDetail) {
			// System.out.println(jsonObject);
			deliverdetailMapper.add(Integer.parseInt(dd), jsonObject.getInteger("goo_id"),
					jsonObject.getInteger("od_yesnum"), jsonObject.getInteger("od_nonum"),
					jsonObject.getBigDecimal("od_price"), jsonObject.getBigDecimal("od_sumprice"),
					jsonObject.getInteger("o_id"));
		}
		logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(), findByIds.getInteger("use_id"), 2203,
				findByIds.getInteger("o_id") + "订单已发货..", "");

	}

	/**
	 * 带条件的查询发货信息
	 * 
	 * @param selects     条件头
	 * @param selectinput 输入的客户名
	 * @param selectend   （废除）
	 * @param currentPage 页码
	 * @param pagesize    页大小
	 * @param uid         用户id
	 * @return
	 */
	public List<JSONObject> selectByPagerSqlFaHuo(String selects, String selectinput, String selectend, int currentPage,
			int pagesize, int uid) {

		// 查询当前员工是老板还是普通员工
		String selectByBoss = userMDAO.selectByBoss(uid);
		if (selectByBoss != null && selectByBoss.length() > 0) {
			if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

				selectinput = "%" + selectinput + "%";
			}
			PageHelper.startPage(currentPage, pagesize);
			return deliverdetailMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, null, null, null);
		} else {
			// 普通员工则先获取，自己正管理的客户
			List<Integer> selectByUserid = customerMapper.selectByUserid(uid);

			// 存储此员工管理的所有的客户Id
			List<Integer> allids = new ArrayList<>();

			if (selectByUserid.size() > 0 && selectByUserid != null) {
				// 当查询的客户id不为空的时候才做
				for (Integer integer : selectByUserid) {
					allids.addAll(alls.findAll(integer, 2));
				}
				if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

					selectinput = "%" + selectinput + "%";
				}
				PageHelper.startPage(currentPage, pagesize);
				return deliverdetailMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, allids, uid,
						1);
			} else {
				return null;
			}

		}

	}

	/**
	 * 查询发货id的对应信息
	 * 
	 * @param id
	 * @return
	 */
	public JSONObject findBydedid(Integer id) {
		return deliverdetailMapper.findByid(id);
	}

	/**
	 * 发货：发货详情商品
	 * 
	 * @param id         发货详情id
	 * @param faid       发货表id
	 * @param havetime   发货时间
	 * @param havegmoney 发货金额
	 * @param oid        订单Id
	 * @param odid       订单详情表id
	 */
	public void doFaHuo(Integer id, Integer faid, Date havetime, double havegmoney, Integer oid, Integer odid) {
		try {
			// 修改当前发货id的商品属性
			int doIdFaHuo = deliverdetailMapper.doIdFaHuo(id, havetime, new BigDecimal(havegmoney));
			// 修改订单明细
			detailMapper.updateByDed(odid);

			// 查询对应发货表id，查看其所有发货商品的发货状态是否是发货，是则修改其总金额等属性
			// 1、得到对应表单
			// 查询对应订单信息
			JSONObject order = orderFormMapper.findByIds(oid);
			// 2、查询当前发货商品数量是否等于订单商品数量
			int findSumQuatity = deliverdetailMapper.findSumQuatity(faid);
			if (findSumQuatity == order.getInteger("o_sumcount")) {
				// 当其数量已经达到了订单需要的总数量，代表订单发货完毕
				Double findUdMoney = deliverdetailMapper.findUdMoney(faid);// 查出对应发货id的总邮递费用
				deliveryscheduleMapper.updateFhDan(new Date(), new BigDecimal(findUdMoney), faid);// 修改发货单
				// 开始判断订单是否已经全部完成
				orderfromServices.statusOrder(oid);
			}
			// 查询对应发货单
			// JSONObject deder = deliveryscheduleMapper.findById(faid);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	/**
	 * 查询对应客户id的订单信息
	 * 
	 * @param ids
	 * @return
	 */
	public List<JSONObject> findOrderByCus(Integer ids) {
		return orderFormMapper.findOrderByCus(ids);
	}

	/**
	 * 带条件的查询退货
	 * 
	 * @param selects     条件头
	 * @param selectinput 输入的客户名
	 * @param selectend   （废除）
	 * @param currentPage 页码
	 * @param pagesize    页大小
	 * @param uid         用户id
	 * @return
	 */
	public List<JSONObject> selectByPagerSqlReturn(String selects, String selectinput, String selectend,
			int currentPage, int pagesize, int uid) {
		/*
		 * if (selectinput.length()>0&&!selectinput.trim().equals("")) {
		 * 
		 * selectinput="%"+selectinput+"%"; } PageHelper.startPage(currentPage,
		 * pagesize); return returnmoreMapper.findByQueryOrder(selects, selectinput,
		 * currentPage, pagesize,null,null,null);
		 */

		// 查询当前员工是老板还是普通员工
		String selectByBoss = userMDAO.selectByBoss(uid);
		if (selectByBoss != null && selectByBoss.length() > 0) {
			if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

				selectinput = "%" + selectinput + "%";
			}
			PageHelper.startPage(currentPage, pagesize);
			return returnmoreMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, null, null, null);
		} else {
			// 存储此员工管理的所有的客户Id
			List<Integer> allids = new ArrayList<>();

			if (selectinput.length() > 0 && !selectinput.trim().equals("")) {

				selectinput = "%" + selectinput + "%";
			}
			PageHelper.startPage(currentPage, pagesize);
			return returnmoreMapper.findByQueryOrder(selects, selectinput, currentPage, pagesize, allids, uid, null);

		}

	}

	/**
	 * 设置联系人地址
	 * 
	 * @param adress
	 * @param id
	 * @param oid
	 * @return
	 */
	public int setAd(String adress, String id, String oid) {
		try {

			System.out.println("我来添加联系人了");

			// 获取id
			Integer findMaxId = addressdetailMapper.findMaxId();
			String ad_id = MaxId.generateNewOrderID(findMaxId);

			// 获取联系人
			JSONObject linkman = linkmanMapper.selectBylinid(Integer.parseInt(id));

			Addresdetail addresdetail = new Addresdetail();
			addresdetail.setAdId(Integer.parseInt(ad_id));
			addresdetail.setAdAddress(adress);
			addresdetail.setAdPhone(linkman.getString("lin_phone"));
			addresdetail.setAdMan(linkman.getString("lin_name"));
			addressDAO.save(addresdetail);

			int result = linkmanMapper.updateadId(ad_id, Integer.parseInt(id));
			System.out.println(ad_id + ".........." + oid);
			System.out.println(ad_id + "------" + id);
			System.out.println(result + "结果");

			return Integer.parseInt(ad_id);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}
}
